package example;

import cn.ctyun.cloudflow.sdk.http.CTApiClient;
import cn.ctyun.cloudflow.sdk.http.Credential;
import cn.ctyun.cloudflow.sdk.http.HttpMethod;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import example.pojo.InvokeFunctionRequest;
import example.pojo.OpenapiResponse;

import java.io.IOException;

public class InvokeFunctionExample {
    /**
     * 华东1资源池ID
     */
    private final static String REGION_HUA_DONG_1 = "bb9fdb42056f11eda1610242ac110002";
    /**
     * 西南1资源池ID
     */
    private final static String REGION_XI_NIAN_1 = "200000002368";

    /**
     * 函数计算执行apigateway地址（西南1）
     */
    private final static String ENDPOINT_CF = "https://cf.fc-xinan1.ctyun.cn";

    private final static ObjectMapper mapper = new ObjectMapper();
    static {
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public static void main(String[] args) throws IOException {
        String ak = "Your-AK";
        String sk = "Your-SK";
        String executionName = "The-executionName"
        String workflowName = "Your-workflowName";
        String intput = "{ \"a\" : 1}"

        CTApiClient client = new CTApiClient.Builder()
                // 天翼云AK、SK，必填
                .withCredential(new Credential(ak, sk))
                // 云工作流 API endpoint，必填
                .withEndpoint(ENDPOINT_CF)
                // Region，必填
                .withRegion(REGION_XI_NIAN_1)
                .build();
        String requestBody = new InvokeFlowRequest(executionName,workflowName,input);
        String responseBody = client.request(
                HttpMethod.POST,
                "/openapi/v1/execution/start",
                null,
                requestBody);
        System.out.println("invoke result: " + responseBody);
    }
}
